/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entityBean.StaffsFacade;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ADMIN
 */
@ManagedBean
@RequestScoped
public class StaffLoginBean {

    @EJB
    private StaffsFacade staffsFacade;
    private String status = "true";
    private String statusloged;

    public String getStatusloged() {
        return statusloged;
    }

    public void setStatusloged(String statusloged) {
        this.statusloged = statusloged;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
    private String stname;

    public String getStname() {
        return stname;
    }

    public void setStname(String stname) {
        this.stname = stname;
    }

    public String getStpass() {
        return stpass;
    }

    public void setStpass(String stpass) {
        this.stpass = stpass;
    }
    private String stpass;

    /** Creates a new instance of StaffLoginBean */
    public StaffLoginBean() {
        //kiem tra dang nhap
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session.getAttribute("status") != null) {
            status = session.getAttribute("status").toString();
            statusloged = "true";
        }
    }

    public String login() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (staffsFacade.login(stname, stpass).size() > 0) {
            session.setAttribute("status", false);

            status = session.getAttribute("status").toString();
            statusloged = "true";
            return "index";
        } else {
            return "loginerror";
        }
    }

    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("status", null);
        status = "true";
        statusloged = "false";
        return "index";
    }
}
